package zhongbo.java.websocket.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.websocket.demo.mapper.HistoryMapper;
import zhongbo.java.websocket.demo.model.History;
import zhongbo.java.websocket.demo.service.IHistoryService;
import zhongbo.java.websocket.demo.vo.PageVo;

import java.util.Date;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年03月20日 星期四 04:19:11
 */
@Service
public class HistoryServiceImpl implements IHistoryService {

    @Autowired
    private HistoryMapper historyMapper;

    @Override
    public History addHistory(History history) {
        if (history.getTime() == null) {
            history.setTime(new Date());
        }
        int insert = historyMapper.insert(history);
        if (insert > 0) {
            return history;
        }
        return null;
    }

    @Override
    public PageVo<History> listHistory(int page, int pageSize) {
        QueryWrapper<History> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        IPage<History> pageParam = new Page<>(page, pageSize);
        IPage<History> historyIPage = historyMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(historyIPage);
    }

    @Override
    public String delete(String id) {
        int delete = historyMapper.deleteById(id);
        if (delete > 0) {
            return id;
        }
        return null;
    }
}
